package com.sangmo.fts.process.receivable.calculator;

import com.sangmo.boot.framework.common.utils.ApplicationUtils;
import com.sangmo.boot.framework.data.json.context.KeyValue;
import com.sangmo.boot.framework.data.json.load.Calculator;
import com.sangmo.fts.process.inventory.model.IfRepayment;
import com.sangmo.fts.process.inventory.repo.IfRepaymentRepo;
import com.sangmo.fts.process.receivable.model.RpfLoan;
import com.sangmo.fts.process.receivable.model.RpfRepayment;
import com.sangmo.fts.process.receivable.repo.RpfRepaymentRepo;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;

@Component
public class RpfLoanRepaymentAmountCalculator implements Calculator<RpfLoan>, RpfLoan.IRpfLoanRepaymentAmountCalculator {

    @Override
    public Object calc(RpfLoan bean, Collection<KeyValue> exts) {
        RpfRepaymentRepo repaymentRepo = ApplicationUtils.getBean(RpfRepaymentRepo.class);
        List<RpfRepayment> repayments = repaymentRepo.findByLoanId(bean.getId());
        BigDecimal repaymentAmount = BigDecimal.ZERO;
        for (RpfRepayment repayment : repayments) {
            repaymentAmount = repaymentAmount.add(repayment.getAmount());
        }
        return repaymentAmount;
    }
}
